package com.boatAnchorAssistant.service.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gitee.sunchenbin.mybatis.actable.utils.ClassScaner;
import com.boatAnchorAssistant.pojo.entity.ExpandField;
import com.boatAnchorAssistant.pojo.param.expandField.*;
import com.boatAnchorAssistant.pojo.vo.expandField.*;
import com.boatAnchorAssistant.pojo.dto.expandField.*;
import com.boatAnchorAssistant.service.repository.in.ExpandFieldRepository;
import com.boatAnchorAssistant.service.service.in.ExpandFieldService;
import nirvana.core.domains.ann.AccEntity;
import nirvana.core.domains.enums.EntityType;
import nirvana.core.domains.vo.PageResult;
import nirvana.core.generateCode.GenerateFile;
import nirvana.core.utils.ids.IdGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * 【描 述】：拓展字段服务
 * 【环 境】：J2SE 17
 * 【详 细】：
 */
@Service
public class ExpandFieldServiceImpl implements ExpandFieldService {

    private final ExpandFieldRepository expandFieldRepository;

    public ExpandFieldServiceImpl(ExpandFieldRepository expandFieldRepository) {
        this.expandFieldRepository = expandFieldRepository;
    }

    /**
     * 创建拓展字段
     *
     * @param param
     */
    @Override
    public void createExpandField(CreateExpandFieldParam param) {
        ExpandField expandField = new ExpandField();
        BeanUtils.copyProperties(param, expandField);
        Long id = IdGenerator.SNOW.generate();
        expandField.setId(id);
        expandFieldRepository.create(expandField);
    }

    /**
     * 保存拓展字段
     *
     * @param param
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveExpandField(SaveExpandFieldParam param) {
        String model = param.getModel();
        expandFieldRepository.clean(model);
        List<SaveExpandFieldItemParam> itemList = param.getItemList();

        List<ExpandField> list = new ArrayList<>();


        for (int i = 0; i < itemList.size(); i++) {
            SaveExpandFieldItemParam item = itemList.get(i);
            ExpandField expandField = new ExpandField();
            BeanUtils.copyProperties(item, expandField);
            expandField.setModel(model);
            expandField.setSort(i + 1);

            list.add(expandField);
        }
        expandFieldRepository.saveBatch(list);
    }

    /**
     * 修改拓展字段
     *
     * @param param
     */
    @Override
    public void editExpandField(EditExpandFieldParam param) {
        Long id = param.getId();
        ExpandField expandField = expandFieldRepository.getByIdCheck(id);
        BeanUtils.copyProperties(param, expandField);
        expandFieldRepository.edit(expandField);
    }

    /**
     * 移除拓展字段
     *
     * @param id
     */
    @Override
    public void removeExpandField(Long id) {
        expandFieldRepository.removeById(id);
    }

    /**
     * 启动/禁用拓展字段
     *
     * @param id
     */
    @Override
    public void enabledExpandField(Long id) {
        ExpandField expandField = expandFieldRepository.getByIdCheck(id);
        expandField.setIsDisable(!expandField.getIsDisable());
        expandFieldRepository.edit(expandField);
    }

    /**
     * 获取拓展字段详情
     *
     * @param id
     * @return
     */
    @Override
    public GetExpandFieldVo getExpandField(Long id) {
        ExpandField expandField = expandFieldRepository.getByIdCheck(id);
        GetExpandFieldVo vo = new GetExpandFieldVo();
        BeanUtils.copyProperties(expandField, vo);
        return vo;
    }

    /**
     * 获取拓展字段 列表
     *
     * @param param
     * @return
     */
    @Override
    public PageResult<GetExpandFieldListVo> getExpandFieldList(GetExpandFieldListParam param) {

        SelectExpandFieldDto dto = new SelectExpandFieldDto();
        BeanUtils.copyProperties(param, dto);
        Page<ExpandField> page = expandFieldRepository.selectExpandField(dto);
        List<ExpandField> expandFieldList = page.getRecords();
        List<GetExpandFieldListVo> list = new ArrayList<>();
        for (ExpandField expandField : expandFieldList) {
            GetExpandFieldListVo vo = new GetExpandFieldListVo();
            BeanUtils.copyProperties(expandField, vo);

            list.add(vo);
        }
        return new PageResult<>(list, page);

    }

    /**
     * 获取拓展字段下拉列表
     *
     * @param param
     * @return
     */
    @Override
    public List<GetExpandFieldDownListVo> getExpandFieldDownList(GetExpandFieldDownListParam param) {
        ExpandField po = new ExpandField();
        if (param != null) {
            BeanUtils.copyProperties(param, po);
        }
        List<ExpandField> expandFieldList = expandFieldRepository.getListByEntity(po);

        List<GetExpandFieldDownListVo> list = new ArrayList<>();
        for (ExpandField expandField : expandFieldList) {
            GetExpandFieldDownListVo vo = new GetExpandFieldDownListVo();
            BeanUtils.copyProperties(expandField, vo);
            list.add(vo);
        }
        return list;
    }

    /**
     * 获取模块列表
     *
     * @return
     */
    @Override
    public List<GetModelListVo> getModelList() {
        String[] packs = {"com.boatAnchorAssistant.pojo"};
        Set<Class> allClasses = ClassScaner.scan(packs, AccEntity.class);


        List<GetModelListVo> list = new ArrayList<>();
        for (Class<?> aClass : allClasses) {

            AccEntity entity = aClass.getAnnotation(AccEntity.class);
            if (!entity.type().equals(EntityType.SERVICE_ENTITY)) {
                continue;
            }
            String simpleName = aClass.getSimpleName();

            GetModelListVo vo = new GetModelListVo();
            vo.setName(entity.value());
            vo.setModel(GenerateFile.convertTheFirstLetterToLowerCase(simpleName));

            list.add(vo);
        }

        return list;
    }


}



